/**
 * @author
 * @description 登录、获取用户信息、退出登录、清除accessToken逻辑，不建议修改
 */

import Vue from 'vue'
import { getUserInfo, login, logout } from '@/api/user'
import {
  getAccessToken,
  removeAccessToken,
  setAccessToken,
} from '@/utils/accessToken'
import { resetRouter } from '@/router'
import { title, tokenName } from '@/config'
import { setToken } from '@/utils/auth'
import router from '@/router'

const state = () => ({
  accessToken: getAccessToken(), //此属性是从 localStorage拿到
  id: '',
  username: '', //用户名称
  nickname: '', //用户昵称
  //avatar: '',头像的请求地址
  permissions: [], //原项目没有此属性-权限,暂时不使用
  menus: [], //可以使用的菜单
  buttons: [], //可以使用的按钮
  roles: [], //当前用户的角色
  routes: [], //当前用户对应的路由
})
const getters = {
  accessToken: (state) => state.accessToken,
  id: (state) => state.id,
  username: (state) => state.username,
  //avatar: (state) => state.avatar,
  permissions: (state) => state.permissions, // 后台请求的路由赋值给该属性
  // menus: (state) => state.menus,
  buttons: (state) => state.buttons, // 后台请求的按钮权限赋值给该属性
  roles: (state) => state.roles,
  routes: (state) => state.routes,
  nickname: (state) => state.nickname,
}
const mutations = {
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken
    setAccessToken(accessToken)
  },
  setId(state, id) {
    state.id = id
  },
  setUsername(state, username) {
    state.username = username
  },
  /*setAvatar(state, avatar) {
    state.avatar = avatar
  },*/
  setPermissions(state, permissions) {
    state.permissions = permissions
  },
  // setMenus(state, menus) {
  //   state.menus = menus
  // },
  setButtons(state, buttons) {
    state.buttons = buttons
  },
  setRoles(state, roles) {
    state.roles = roles
  },
  setRoutes(state, routes) {
    state.routes = routes
  },
  setNickname(state, nickname) {
    state.nickname = nickname
  },
}
const actions = {
  setPermissions({ commit }, permissions) {
    commit('setPermissions', permissions)
  },
  setButtons({ commit }, buttons) {
    commit('setButtons', buttons)
  },
  async login({ commit }, userInfo) {
    const res = await login(userInfo)
    console.log(res)
    if (res.code == 200) {
      const data = res.data
      const accessToken = data[tokenName]
      commit('setAccessToken', accessToken) //给localStorage里面存入当前用户的token
      setToken(accessToken) //给cookie中存入当前用户的token
    } else {
      Vue.prototype.$baseMessage(`${res.message}`, 'error')
    }
  },

  //获取用户个人信息
  async getUserInfo({ commit, state }) {
    const { data } = await getUserInfo(state.accessToken)
    if (!data) {
      Vue.prototype.$baseMessage('验证失败，请重新登录...', 'error')
      commit('setAccessToken', '')
      return false
    }
    const { id, roles, userName, nickName } = data
    // 判断当前用户是否具有角色
    if (roles && roles.length > 0) {
      roles.forEach((item) => {
        item = item.roleCode
      })
      //commit('setPermissions', '[admin]')
      //commit('setAvatar', avatar)
      commit('setRoles', roles)
      commit('setId', id)
      commit('setUsername', userName)
      // commit('setButtons', buttons)
      // commit('setMenus', menus)
      // commit('setRoutes', routes)
      commit('setNickname', nickName)
      //返回路由，用于动态路由
      // console.log(routes)
      return userName
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      commit('setAccessToken', '')
      return 'noroles'
    }
  },

  async logout({ dispatch }) {
    await logout(state.accessToken)
    await dispatch('resetAccessToken')
    await resetRouter()
  },
  resetAccessToken({ commit }) {
    commit('setRoles', [])
    // commit('setPermissions', [])
    commit('setButtons', [])
    commit('setAccessToken', '')
    removeAccessToken()
  },
}
export default { state, getters, mutations, actions }
